﻿using BadOCR.Runtime;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace BadOCR.View.ViewModel
{
    public class UserConfigViewModel : ViewModelBase
    {
        #region BindingProperty
        // 截图翻译
        private List<dynamic> _allOCREngineKinds;
        public List<dynamic> AllOCREngineKinds
        {
            get
            {
                if (_allOCREngineKinds == null)
                {
                    _allOCREngineKinds = new List<dynamic>()
                    {
                        new {
                            Value = OCREngineKind.Sougou,
                            Display = "搜狗OCR引擎"
                        },
                        new {
                            Value = OCREngineKind.SpaceOCR,
                            Display = "SpaceOCR引擎"
                        },
                        new {
                            Value = OCREngineKind.Baidu,
                            Display = "百度OCR引擎"
                        }
                    };
                }

                return _allOCREngineKinds;
            }
        }

        private dynamic _selectedOCREngineKind;
        public dynamic SelectedOCREngineKind
        {
            get => _selectedOCREngineKind;
            set
            {
                _selectedOCREngineKind = value;
                RaisePropertyChanged(nameof(SelectedOCREngineKind));
            }
        }

        private string _clientId;
        public string ClientId
        {
            get => _clientId;
            set
            {
                _clientId = value;
                RaisePropertyChanged(nameof(ClientId));
            }
        }

        private string _clientSecret;
        public string ClientSecret
        {
            get => _clientSecret;
            set
            {
                _clientSecret = value;
                RaisePropertyChanged(nameof(ClientSecret));
            }
        }

        private bool _autoSave;
        public bool AutoSave
        {
            get => _autoSave;
            set
            {
                _autoSave = value;
                RaisePropertyChanged(nameof(AutoSave));
            }
        }

        // 语言偏好
        private List<dynamic> _allLanguageKinds;
        public List<dynamic> AllLanguageKinds
        {
            get
            {
                if (_allLanguageKinds == null)
                {
                    _allLanguageKinds = new List<dynamic>()
                    {
                        new {
                            Value = LanguageKind.SimplifiedChinese,
                            Display = "中文"
                        },
                        new {
                            Value = LanguageKind.English,
                            Display = "英语"
                        },
                        new {
                            Value = LanguageKind.Japanese,
                            Display = "日语"
                        },
                        new {
                            Value = LanguageKind.Korean,
                            Display = "韩语"
                        }
                    };
                }

                return _allLanguageKinds;
            }
        }

        private dynamic _selectedMotherLanguage;
        public dynamic SelectedMotherLanguage
        {
            get => _selectedMotherLanguage;
            set
            {
                _selectedMotherLanguage = value;
                RaisePropertyChanged(nameof(SelectedMotherLanguage));
            }
        }

        private dynamic _selectedFirstForeignLanguage;
        public dynamic SelectedFirstForeignLanguage
        {
            get => _selectedFirstForeignLanguage;
            set
            {
                _selectedFirstForeignLanguage = value;
                RaisePropertyChanged(nameof(SelectedFirstForeignLanguage));
            }
        }
        #endregion

        #region BindingCommand
        private RelayCommand _updateUserConfigCommand;
        public ICommand UpdateUserConfigCommand
        {
            get
            {
                if (_updateUserConfigCommand == null)
                    _updateUserConfigCommand = new RelayCommand(() => UpdateUserConfig());

                return _updateUserConfigCommand;
            }
        }

        private void UpdateUserConfig()
        {
            UserConfig.Current.OCREngineKind = SelectedOCREngineKind.Value;
            if (UserConfig.Current.OCREngineKind == OCREngineKind.Baidu)
            {
                UserConfig.Current.ClientId = ClientId;
                UserConfig.Current.ClientSecret = ClientSecret;
            }
            UserConfig.Current.MotherLanguage = SelectedMotherLanguage.Value;
            UserConfig.Current.FirstForeignLanguage = SelectedFirstForeignLanguage.Value;
            UserConfig.Current.AutoSave = AutoSave;

            UserConfig.Current.Save();
            MessageBox.Show("保存成功", "提示");
        }

        private RelayCommand<RoutedEventArgs> _windowLoaded;
        public ICommand WindowLoadedCommand
        {
            get
            {
                if (_windowLoaded == null)
                    _windowLoaded = new RelayCommand<RoutedEventArgs>(OnWindowLoaded);

                return _windowLoaded;
            }
        }

        private void OnWindowLoaded(RoutedEventArgs e)
        {
            // 截图翻译
            SelectedOCREngineKind = AllOCREngineKinds.First(engine => engine.Value == UserConfig.Current.OCREngineKind);

            if (UserConfig.Current.OCREngineKind == OCREngineKind.Baidu)
            {
                ClientId = UserConfig.Current.ClientId;
                ClientSecret = UserConfig.Current.ClientSecret;
            }

            // 语言偏好
            SelectedMotherLanguage = AllLanguageKinds.First(language => language.Value == UserConfig.Current.MotherLanguage);
            SelectedFirstForeignLanguage = AllLanguageKinds.First(language => language.Value == UserConfig.Current.FirstForeignLanguage);
            AutoSave = UserConfig.Current.AutoSave;
        }
        #endregion
    }
}
